package service_blog_mirchen_com

import (
	"gen-go/BlogServices"
	"github.com/jinzhu/gorm"
	"fmt"
)

//service结构
type CategoryServiceHandler struct {
	orm *gorm.DB
}

//文章模型结构
type Category struct {
	Id          int64  `gorm:"primary_key"`
	Catname     string
	Alisaname   string
	Parentid    int64
	Keywords    string
	Description string
	Sort        int64
}

func NewCategoryServiceHandler(getOrm *gorm.DB) *CategoryServiceHandler {
	return &CategoryServiceHandler{orm:getOrm}
}

func (csh *CategoryServiceHandler) GetCategorys() (r []*BlogServices.Category, err error) {
	categorys := &[]Category{}
	err = csh.orm.Where("1=1").Find(categorys).Error
	if err != nil {
		r = nil
	} else {
		r = []*BlogServices.Category{}
		if len(*categorys) > 0 {
			for _, category := range *categorys {
				r = append(r, &BlogServices.Category{
					ID:category.Id,
					CatName:category.Catname,
					AlisaName:category.Alisaname,
					Keywords:category.Keywords,
					Description:category.Description,
					Sort:category.Sort,
					ParentId:category.Parentid,
				})
			}
		}
	}
	return r, err
}

func (csh *CategoryServiceHandler) GetCategoryInfoById(id int64) (r *BlogServices.Category, err error) {
	category := &Category{}
	err = csh.orm.Where("id=?", id).First(category).Error
	if err != nil {
		r = nil
	} else {
		r = &BlogServices.Category{
			ID:category.Id,
			CatName:category.Catname,
			AlisaName:category.Alisaname,
			Keywords:category.Keywords,
			Description:category.Description,
			Sort:category.Sort,
			ParentId:category.Parentid,
		}
	}
	fmt.Println(id)
	return r, err
}
